// FlagBox.cpp: implementation of the CFlagBox class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "8085.h"
#include "FlagBox.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFlagBox::CFlagBox()
{
    Init();
}

CFlagBox::~CFlagBox()
{
    if(m_aNewValue!=NULL) delete [] m_aNewValue;
}

void CFlagBox::Init()
{
    m_aNewValue = NULL;
}

void CFlagBox::SetIndividualProperty()
{
    m_aNewValue = new int[m_nItems*2];
}

int CFlagBox::RefreshStrings(int addr,BOOL bCheckModified)
{
    if(m_aDisplay==NULL) return 0;
    int nIndex=0;

    sprintf(m_aDisplay[nIndex].szMnemonic,"S  %.1X",m_aNewValue[nIndex]=Get8085Object()->GetSign());
    ++nIndex;

    sprintf(m_aDisplay[nIndex].szMnemonic,"Z  %.1X",m_aNewValue[nIndex]=Get8085Object()->GetZero());
    ++nIndex;
    
    sprintf(m_aDisplay[nIndex].szMnemonic,"AC %.1X",m_aNewValue[nIndex]=Get8085Object()->GetAuxCarry());
    ++nIndex;
    
    sprintf(m_aDisplay[nIndex].szMnemonic,"P  %.1X",m_aNewValue[nIndex]=Get8085Object()->GetParity());
    ++nIndex;
    
    sprintf(m_aDisplay[nIndex].szMnemonic,"CY %.1X",m_aNewValue[nIndex]=Get8085Object()->GetCarry());
    ++nIndex;

    m_nItemsDisplayed = nIndex;

    for(int i=0; i<nIndex ; i++)
    {
        if(bCheckModified)
        {
                if(m_aDisplay[i].data != m_aNewValue[i])
				{
                    m_aDisplay[i].bModified = 1;
				}
                else 
					m_aDisplay[i].bModified = 0;
        }
        else m_aDisplay[i].bModified = 0;
        m_aDisplay[i].data = m_aNewValue[i];
    }
    return 1;
}

byte CFlagBox::GetFlagValue(int nIndex)
{
    byte data;
    switch(nIndex)
    {
    case 0    : data=Get8085Object()->GetSign();break;
    case 1    : data=Get8085Object()->GetZero();break;
    case 2    : data=Get8085Object()->GetAuxCarry();break;
    case 3    : data=Get8085Object()->GetParity();break;
    case 4    : data=Get8085Object()->GetCarry();break;
    }
    return data;
}

char * CFlagBox::GetFlagName(int nIndex)
{
    static char str[4];
    str[1] = '\0';

    switch(nIndex)
    {
    case 0    : return "S";
    case 1    : return "Z";
    case 2    : return "AC";
    case 3    : return "P";
    case 4    : return "C";
    }
    return str;
}


void CFlagBox::SetFlagValue(int nIndex, byte data)
{
    switch(nIndex)
    {
    case 0    : Get8085Object()->SetSign(data);break;
    case 1    : Get8085Object()->SetZero(data);break;
    case 2    : Get8085Object()->SetAuxCarry(data);break;
    case 3    : Get8085Object()->SetParity(data);break;
    case 4    : Get8085Object()->SetCarry(data);break;
    }
}

void CFlagBox::ToggleFlag()
{
    SetFlagValue(m_nSelectedIndex,!GetFlagValue(m_nSelectedIndex));
}

void CFlagBox::IncrementData()
{
    ToggleFlag();
}

void CFlagBox::DecrementData()
{
    ToggleFlag();
}


void CFlagBox::EnterData()
{
    ToggleFlag();
    Refresh();
}
